<template>
	<view class="content">
		<image class="logo" src="/static/logo.png"></image>
		<input class="uni-input" v-model="username" placeholder="请输入学号" />
		<input class="uni-input" type="password" v-model="password" placeholder="请输入密码" />

		<button v-on:click="loginf">登录</button>
	</view>
</template>

<script>
	import isDate from '@/utils/upDate.js'
	import {
		login
	} from '@/u_api/login.js'
	import {
		encrypt
	} from '../../utils/encryp.js'
	export default {
		data() {
			return {
				username: "",
				password: ""
			}
		},
		beforeCreate() {
			//判断登录
			isDate();
			const jwt = uni.getStorageSync("jwt");
			const user = uni.getStorageSync('user') 
			if (jwt && user) {
				uni.switchTab({
					url: "/pages/timetable/timetable"
				})
			}
		},
		mounted() {
			var a = document.getElementsByClassName('uni-page-head-hd')[0]
			a.style.display = 'none';
		},
		methods: {
			jump(url) {
				uni.switchTab({
					url
				})
			},
			async loginf() {

				if (this.username == "" || this.password == "") {
					uni.showModal({
						title: '学号或密码为空',
						content: '请输入学号或密码',
					});
					return;
				}

				const result = await login(encrypt(this.username), encrypt(this.password))
				//登录失败
				//console.log(result);
				if (result.err === "1" || result.msg == false || result.msg === "错误")
				{
					uni.showModal({
						title: "提示",
						content: '学号或密码错误',
						success: () => {
							this.password = "";
						},
					});
					return;
				}
				// 登录成功
				if (result.err === 0)
					try {
						uni.setStorageSync('jwt', result.msg);
						uni.setStorageSync('user', result.user);
						this.jump("/pages/timetable/timetable")
					} catch (e) {
						console.log(e)
					}

			}
		}

	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.content input {
		padding-left: 10px;
		margin-top: 20px;
		box-sizing: border-box;
		width: 68%;
		height: 40px;
		border: 2px solid #32a1ce;
		border-radius: 8px;
	}

	.content button {

		margin-top: 30px;
		width: 68%;
		border-radius: 8px;
		color: white;
		background-color: #2b9939;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		border-radius: 20px;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}
</style>
